﻿using DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using ClassGen;

namespace KhoaLuan.Utils
{
    public class SecurityServices
    {

        public static bool HasPermison(TypeAudit audit, int Permison)
        {
            if (((int)audit & Permison) == (int)audit)

                return true;
            return false;
        }

        public static bool HasPermison(string Link, int Userid, TypeAudit permission)
        {
            var menuproxy = new tbMenu_Proxy();
            var groupuserproxy = new tbGroupUser_Proxy();
            var powerproxy = new tbPowers_Proxy();
            var menu = menuproxy.GetByLink(Link);

            if (menu != null)
            {
                var listgroup = groupuserproxy.GetByUser(Userid).Select(x=>x.GroupId);
                var listpowreformenu = powerproxy.GetByMenu(menu.MenuID);
                foreach (var item in listpowreformenu)
                {
                    if(listgroup.Contains(item.GroupID))
                    {
                        if(HasPermison(permission,item.NumberPermison))
                            return true;
                    }
                }
                return false;                
            }
            else return true;
        }
    }
}